const express = require("express");

const mongoose = require("mongoose");

const bodyParser = require("body-parser");

const passport = require("passport");

// passport-jwt passport

//引入路由
const user = require("./routes/api/user");
const profile = require("./routes/api/profile");

// db config
const dbUrl = require("./config/key").mongoUrl;

const app = express();

// 使用bodyParser中间件
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

mongoose
	.connect(dbUrl)
	.then((result) => {
		console.log("result");
	})
	.catch((err) => {
		console.log("err", err);
	});

app.use(passport.initialize());

require("./config/passport")(passport);

app.get("/", (req, res) => {
	res.send("hello world!");
});

// 使用路由
app.use("/api/user", user);
app.use("/api/profile", profile);

const port = process.env.PORT || 5000;

app.listen(port, () => {
	console.log(`Server runing on port ${port}`);
});
